//
//  main.c
//  Random
//
//  Created by edz on 2020/7/15.
//  Copyright © 2020 edz. All rights reserved.
//

// 生成不重复的随机数
#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[]) {
    int n = 6;
    int a[6] = {0};
    int i;
    int r = 0;
    for (i = 0 ; i < n; i++) {
        a[i] = i+1;
    }
    for (i = 0 ; i < n; i++) {
        r = rand()%(n-i) + i;
        int temp = a[i];
        a[i] = a[r];
        a[r] = temp;
    }
    for (i = 0 ; i < n; i++) {
        printf("%2d",a[i]);
    }
    printf("\n");
    return 0;
}



/*
 // java的两种方式
 public class Main {

     public static void main(String[] args) {
         Random random = new Random();
         LinkedHashSet<Integer> a = new LinkedHashSet<>();
         while (a.size() < 6){
             int i = random.nextInt(6);
             a.add(i);
         }
         System.out.println(a);

         ArrayList<Integer> b = new ArrayList<>();
         boolean[] bool = new boolean[6];
         int i = 0;
         while (b.size() < 6){
             do {
                 i = random.nextInt(6);
             }while (bool[i]);
             bool[i] = true;
             b.add(i);
         }
         System.out.println(b);
     }
 }
 */
